/**
 * 替换字符串中所有匹配的字符
 * @param {string} str - 源字符串
 * @param {string} reallyDo - 将要被替换的字符串
 * @param {string} replaceWith - 取代关键字的新字符串
 * @param {string} ignoreCase - 替换模式。默认为gi，全局匹配且不区分大小写
 * @returns {string}
 */
function replaceAll(str, reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
        return str.replace(new RegExp(reallyDo, (ignoreCase ? "g" : "gi")), replaceWith);
    } else {
        return str.replace(reallyDo, replaceWith);
    }
}


/**
 * 首字母小写
 * @returns {string}
 */
function firstLowerCase(str) {
    return str.replace(/\b(\w)(\w*)/g, function ($0, $1, $2) {
        return $1.toLowerCase() + $2;
    });
}

/**
 * 首字母大写
 * @returns {string}
 */
function firstUpperCase(str) {
    return str.replace(/\b(\w)(\w*)/g, function ($0, $1, $2) {
        return $1.toUpperCase() + $2;
    });
}

/**
 * 隐藏中间的字符串
 */
function hideMiddle(str) {
    if (str.length > 2) {
        return  str[0] + '**' + str[str.length - 1]
    } else {
        return str;
    }
}

/**
 * 仅显示尾数
 * @param str
 * @param len
 * @returns {string|*}
 */
function onlyRight(str, len = 4) {
    if (str.length <= len) {
        return str;
    }
    return '*' + str.substr(str.length - len, len);
}

export {
    replaceAll,
    firstLowerCase,
    firstUpperCase,
    hideMiddle,
    onlyRight
}
